package org.baicaixiaozhan.learn.guava.base;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import java.util.function.Consumer;


/**
 * DESC: {@code Functional Programming} 示例
 *
 * @author : baicaixiaozhan
 * @since : 2020-11-12
 */
public class FunctionalDemo {

    public static void main(String[] args) {

        String msg = "alert this massage";

        finalPrint.accept(printInGuava.apply(msg));
        finalPrint.accept(printJava.apply(msg));

        finalPrint.accept("Predicate in Java: " + lengthOutOfBoundsInJava.test(msg));
        finalPrint.accept("Predicate in Guava: " + lengthOutOfBoundsInJava.test(msg));
    }
    // Predicate
    /** 最大长度 */
    static final Integer MAX_LENGTH = 5;

    static Predicate<CharSequence> lengthOutOfBoundsInGuava = FunctionalDemo::lengthOutOfBounds;

    static java.util.function.Predicate<CharSequence> lengthOutOfBoundsInJava = FunctionalDemo::lengthOutOfBounds;

    public static Boolean lengthOutOfBounds(CharSequence input) {
        Preconditions.checkNotNull(input, "not null / ''");
        return input.length() > MAX_LENGTH;
    }

    // Functional
    static Function<CharSequence, CharSequence> printInGuava = FunctionalDemo::doSting;

    static java.util.function.Function<CharSequence, CharSequence> printJava = FunctionalDemo::doSting;

    static Consumer<CharSequence> finalPrint = System.out::println;

    public static CharSequence doSting(CharSequence input) {
        Preconditions.checkNotNull(input, "not null / ''");
        return input + (" ====");
    };
}
